﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsGetManageDAL.cs
/// 作  者：黄纲
/// 时  间：2007.01.10
/// 说  明：ClsGetManageDAL类用于应收管理信息相关处理
/// </summary>
public class ClsGetManageDAL
{
    #region  数据库连接字符串
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    #endregion

    /// <summary>
    /// 获取应收主单信息
    /// </summary>
    /// <param name="G_Type">类别</param>
    /// <param name="Param">查询参数</param>
    /// <param name="UserID">当前登陆用户ID</param>
    /// <param name="QueryMode">查询方式(0为精确，1为模糊)</param>
    /// <param name="Type">返回数据集类型</param>
    /// <returns>返回数据集</returns>
    public DataSet GetGetManageDataSet(int G_Type, string Param, string UserID, int QueryMode, int Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetGetManageDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@g_type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@g_type"].Value = G_Type;
        dat.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@param"].Value = Param;
        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@u_log_id"].Value = UserID;
        dat.SelectCommand.Parameters.Add("@mode", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@mode"].Value = QueryMode;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@type"].Value = Type;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有应收从表信息
    /// <param name="Type"></param>
    /// <param name="No">应收单号</param>
    /// </summary>
    /// <returns>返回数据集</returns>
    public DataSet GetGetManageChildDataSet(int Type, string No)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetGetManageChildDataSet", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@type"].Value = Type;
        dat.SelectCommand.Parameters.Add("@g_no", SqlDbType.NVarChar, 50);
        dat.SelectCommand.Parameters["@g_no"].Value = No;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 新增或修应收主单信息
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Type"></param>
    /// <param name="No"></param>
    /// <param name="Date"></param>
    /// <param name="Cus_No"></param>
    /// <param name="Dp_No"></param>
    /// <param name="Wk_No"></param>
    /// <param name="Oper"></param>
    /// <param name="Remark"></param>
    /// <returns></returns>
    public Boolean AddOrUpdateInfo(int ID, int Type, string No, DateTime Date, string Cus_No, string Dp_No, string Wk_No, string Oper, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddOrUpdateInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;
        com.Parameters.Add("@no", SqlDbType.NVarChar, 50);
        com.Parameters["@no"].Value = No;
        com.Parameters.Add("@date", SqlDbType.DateTime, 8);
        com.Parameters["@date"].Value = Date;
        com.Parameters.Add("@cus_no", SqlDbType.NVarChar, 50);
        com.Parameters["@cus_no"].Value = Cus_No;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 50);
        com.Parameters["@dp_id"].Value = Dp_No;
        com.Parameters.Add("@wk_id", SqlDbType.NVarChar, 50);
        com.Parameters["@wk_id"].Value = Wk_No;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 50);
        com.Parameters["@oper"].Value = Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增或修改应收从单信息
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="No"></param>
    /// <param name="Im_No"></param>
    /// <param name="Price"></param>
    /// <param name="Qty"></param>
    /// <param name="Agio"></param>
    /// <param name="Agio1"></param>
    /// <param name="Remark"></param>
    /// <returns></returns>
    public Boolean AddOrUpdateChild(int ID, int Type, string No, string Im_No, decimal Price, decimal Qty, decimal Agio, decimal Agio1, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddOrUpdateChild", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;
        com.Parameters.Add("@no", SqlDbType.NVarChar, 50);
        com.Parameters["@no"].Value = No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 50);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@price", SqlDbType.Decimal, 28);
        com.Parameters["@price"].Value = Price;
        com.Parameters.Add("@qty", SqlDbType.Decimal, 28);
        com.Parameters["@qty"].Value = Qty;
        com.Parameters.Add("@agio", SqlDbType.Decimal, 28);
        com.Parameters["@agio"].Value = Agio;
        com.Parameters.Add("@agio1", SqlDbType.Decimal, 28);
        com.Parameters["@agio1"].Value = Agio1;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 删除应收单
    /// </summary>
    /// <param name="ID">应收单ID</param>
    /// <param name="Type">0作废，1彻底删除</param>
    /// <returns></returns>
    public Boolean DeleteInfo(int ID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteGetManageInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 删除应收从单
    /// </summary>
    /// <param name="ID">应收从单ID</param>
    /// <param name="Type">0作废，1彻底删除</param>
    /// <returns></returns>
    public Boolean DeleteChildInfo(int ID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteGetManageChildInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认或取消确认核应收
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="UserID"></param>
    /// <param name="Type"></param>
    /// <returns></returns>
    public Boolean AuditOrOAuditGetInfo(int ID, string UserID, int Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditOrOAuditGetInfo", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@id", SqlDbType.Int, 4);
        com.Parameters["@id"].Value = ID;
        com.Parameters.Add("@user", SqlDbType.NVarChar, 50);
        com.Parameters["@user"].Value = UserID;
        com.Parameters.Add("@type", SqlDbType.Int, 4);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 判断同一应收单中是否存在同类商品
    /// </summary>
    /// <param name="No"></param>
    /// <param name="Im_No"></param>
    /// <returns></returns>
    public int ChildExistSameMateriel(int Type, string No, string Im_No)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_ChildExistSameMateriel", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@g_type", SqlDbType.Int, 4);
        com.Parameters["@g_type"].Value = Type;
        com.Parameters.Add("@g_no", SqlDbType.NVarChar, 50);
        com.Parameters["@g_no"].Value = No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 50);
        com.Parameters["@im_no"].Value = Im_No;

        try
        {
            con.Open();
            return Int32.Parse(com.ExecuteScalar().ToString());
        }
        catch
        {
            return -1;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}
